<?php
/* @var $this SiteController */
//
//$this->pageTitle=Yii::app()->name . ' - About';
//$this->breadcrumbs=array(
//	'About',
//);
?>


<div id="excel-wrapper" class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-primary">
                <div class="panel-heading">项目排期</div>
                <div class="panel-body">
                    <div id="excel-container">

                    </div>
                </div>
            </div>

        </div>

    </div>
</div>
<script>

    $(function() {



//
//        var sheetContent  //数据内容
//                , $container = $("#excel-container")
//                , handsontable = $container.data('handsontable');
        //        , blankContent = handsontable.getData();
        loadExcel();



    });
    function loadExcel() {


        $.ajax({
            type: "post",
            url: "/testdrive/index.php/schedule/getProjectAndDateline",
            data: "",
            success: function(str) {
             //   alert(str);//测试输出内容

                var object = JSON.parse(str);

              //  alert(JSON.stringify(object.projectlist));//测试项目列表
              
                var prolength = object.projectlist.length;//项目数目
                var datelength=object.datelinelist.length;//日期数目
                alert(datelength);

//                var colHeader = ["日期", "任务一", "任务二", "任务三", "任务四", "任务五"];// [12, 23, 5, 3, 25, 98];//
//
//                colHeader.push("任务六");
//                var column = [{data: "dateline"}, {data: "one"}, {data: "two"}, {data: "three"}, {data: "four"}, {data: "five"}];
//                column.push({data: "six"});
                var colHeader = ["日期"];
                var column = [{data: "dateline"}];
                var rowHeader=[];
                for (var i = 0; i < prolength; i++) {
                    column.push({data: (object.projectlist)[i].project});
                    colHeader.push(object.projectlist[i].project);
                }
                 for (var i = 0; i < datelength; i++) {
                   
                    rowHeader.push(object.datelinelist[i].dateline);
                }

                buildExcel(colHeader,rowHeader,column);
                var sheetContent  //数据内容
                        , $container = $("#excel-container")
                        , handsontable = $container.data('handsontable');

                loadAllContent(handsontable);




            },
            error: function() {
                alert("不能获得值");
            }
        }
        );



    }

    //加载表格内容
    function loadAllContent(handsontable) {



        $.ajax({
            type: "post",
            url: "/testdrive/index.php/schedule/newschedulelist",
            data: "",
            success: function(str) {
                // alert(str);
                teststr = str;
                var object = JSON.parse(str);

                handsontable.loadData(object);

                //     var ht = $('#excel-container').data('handsontable');
                //ht.setDataAtCell(0, 0, 'new value');

                // handsontable.alter('insert_col', null, 1, 'colHeaders:"任务六"');

            },
            error: function() {
                alert("不能获得值");
            }
        }
        );

    }
    //加载表格内容
    function loadContent(handsontable) {
        $.ajax({
            type: "post",
            url: "/testdrive/index.php/schedule/schedulelist",
            data: "",
            success: function(str) {
                // alert(str);
                teststr = str;
                var object = JSON.parse(str);
                handsontable.loadData(object);

            },
            error: function() {
                alert("不能获得值");
            }
        }
        );

    }

    function buildExcel(colHeader,rowHeader,column) {
        var data = [
            {dateline: "", one: "", two: "", three: "", four: "", five: ""}
        ];
//        var colHeader = [12, 23, 5, 3, 25, 98];//["日期", "任务一", "任务二", "任务三", "任务四", "任务五"],
//        var column = [{data: "dateline"}, {data: "one"}, {data: "two"}, {data: "three"}, {data: "four"}, {data: "five", }];
        var $tmpExcel = $('#excel-container');
        $tmpExcel.handsontable({
            data: data,
            minSpareRows: 1,
            //   colHeaders: ["日期", "任务一", "任务二", "任务三", "任务四", "任务五"],
            colHeaders: colHeader,
            rowHeaders: rowHeader,
            fixedColumnsLeft: 1,
            //      contextMenu: true,
            columnSorting: true,
            manualColumnResize: true,
            manualRowResize: true,
            persistentState: true,
            columns: column
        });
    }
</script>
